package com.example.xui.Main;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xui.CarView.CardItem;
import com.example.xui.CarView.CardPagerAdapter;
import com.example.xui.CarView.ShadowTransformer;
import com.example.xui.Picture_Selection;
import com.example.xui.R;
import com.example.xui.Shezhi;
import com.example.xui.Way.FallObject;
import com.example.xui.Way.Falling;
import com.xuexiang.xui.widget.flowlayout.FlowTagLayout;
import com.xuexiang.xui.widget.imageview.RadiusImageView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class FourFragment extends Fragment {
    private Falling fallingView;
    private static int i=R.drawable.toxiang;
    private RadiusImageView radiusImageView;
    private TextView id;
    String sno;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_four, container, false);
        radiusImageView=view.findViewById(R.id.radiusImageView);
        id=view.findViewById(R.id.textView9);
        Bundle bundle=this.getArguments();
        int i1=R.drawable.toxiang;
        if(bundle!=null) {
            i1 = bundle.getInt("toxiang", R.drawable.toxiang);
          //  sno=bundle.getString("sno");
            Log.i("TAG","tag1="+i1);
        }
        radiusImageView.setImageResource(i1);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       init();
      /*  mButton = (Button) findViewById(R.id.cardTypeBtn);
        ((CheckBox) findViewById(R.id.checkBox)).setOnCheckedChangeListener(this);*/
        //  mButton.setOnClickListener(this);

    }
    public void init(){
        Bundle bundle=getArguments();
        sno=bundle.getString("sno");
        Toast.makeText(getContext(),"sno="+sno,Toast.LENGTH_SHORT).show();
        TextView textView1 =getActivity().findViewById(R.id.textView16);
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(),Shezhi.class);
                intent.putExtra("userID",sno);
                startActivity(intent);
            }
        });
        FallObject.Builder builder = new FallObject.Builder(getResources().getDrawable(R.drawable.ic_snow));
        FallObject fallObject = builder
                .setSpeed(5,true)
                .setSize(50,50,true)
                .setWind(5,true,true)
                .build();

        fallingView = getActivity().findViewById(R.id.falling);
        fallingView.addFallObject(fallObject,50);//添加50个下落物体对象
       /* Bundle bundle = getArguments();
        if(bundle!=null) {
            i = bundle.getInt("toxiang", R.drawable.toxiang);
        }*/
        Log.i("TAG","tag="+i);
        radiusImageView=getActivity().findViewById(R.id.radiusImageView);
   //     radiusImageView.setImageResource(i);
        radiusImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), Picture_Selection.class);
                startActivity(intent);
            }
        });
        image_update(sno);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public static FourFragment newInstance() {
        FourFragment fragment1 = new FourFragment();
        return fragment1;
    }

    public void image_update(String suo){
        new Thread(){
            @Override
            public void run() {
                super.run();
                RequestParams params = new RequestParams("http://10.138.87.49:8080/user/userselect");
                params.addHeader("Content-Type", "application/x-www-form-urlencoded");
                params.addBodyParameter("userID",suo);
                x.http().post(params, new Callback.CacheCallback<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject result) {
                        try {
                            radiusImageView.setImageBitmap(BitmapFactory.decodeFile(result.getString("userPicture")));
                            id.setText(result.getString("userNickname"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Log.i("dsa",params.toString());
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public boolean onCache(JSONObject result) {
                        return false;
                    }
                });
            }
        }.start();
    }

}
